package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.Drug;
import com.ruoyi.project.drug.service.IDrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/drug/pill")
public class DrugController extends BaseController {
    @Autowired
    private IDrugService iDrugService;

    @PreAuthorize("@ss.hasPermi('drug:pill:query')")
    @GetMapping("/list")
    public TableDataInfo listDrugs(Drug drug){
        startPage();
        return getDataTable(iDrugService.findDrugs(drug));
    }

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('drug:pill:add')")
    public AjaxResult addDrug(@RequestBody Drug drug){
        drug.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iDrugService.insertDrug(drug));
    }

    @GetMapping("/{drugId}")
    @PreAuthorize("@ss.hasPermi('drug:pill:query')")
    public AjaxResult getByDrugId(@PathVariable Long drugId){
        return AjaxResult.success(iDrugService.selectDrug(drugId));
    }

    @PutMapping
    @PreAuthorize("@ss.hasPermi('drug:pill:edit')")
    public AjaxResult updateDrug(@RequestBody Drug drug){
        drug.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iDrugService.alterDrug(drug));
    }

    @DeleteMapping("/{drugIds}")
    @PreAuthorize("@ss.hasPermi('drug:pill:remove')")
    public AjaxResult deleteDrugs(@PathVariable Long[] drugIds){
        return AjaxResult.success(iDrugService.removeDrugs(drugIds));
    }
}
